package com.lanlian.cayman.widget;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.R;

import java.util.ArrayList;

/**
 * 该类用于顶部导航栏
 */
public class TopView extends LinearLayout {

    private Activity context;
    private ArrayList<String> list;
    private ArrayList<View> views = new ArrayList<>();

    private int tv_color_select = 0xFFFFFFFF;
    private int tv_color_select_un = 0xFFA1ADC8;

    onClickCall call;

    public TopView(Context context) {
        super(context);
        this.context = (Activity) context;
    }

    public TopView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = (Activity) context;
    }

    public void setSelectColor(int color) {
        tv_color_select = color;
    }

    public interface onClickCall {
        void back(int index);
    }

    private void initView(int index) {
        removeAllViews();
        views.clear();
        LinearLayout ll = new LinearLayout(context);
        for (int i = 0; i < list.size(); i++) {
            LayoutInflater inflater = context.getLayoutInflater();
            View view = inflater.inflate(R.layout.view_top, ll, false);
            TextView tv = view.findViewById(R.id.tv);
            tv.setText(list.get(i));
            final int a = i;
            if (i == index) {
                tv.setTextColor(tv_color_select);
            } else {
                tv.setTextColor(tv_color_select_un);
            }
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectView(a);
                    call.back(a);
                }
            });
            addView(view);
            views.add(view);
        }
    }

    private void selectView(int a) {
        for (View view : views) {
            TextView tv = view.findViewById(R.id.tv);
            tv.setTextColor(tv_color_select_un);
        }
        TextView tv = views.get(a).findViewById(R.id.tv);
        tv.setTextColor(tv_color_select);
    }

    public void setData(ArrayList<String> list, onClickCall call, int index) {
        this.list = list;
        this.call = call;
        initView(index);
    }

}
